package serverCommunicator;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * the main of the Server. wait for client to this port, 
 * 			open thread to handle his request and continue to sleep until another request.   
 * @author Orit
 *
 */
public class ServerMain {
    private ServerSocket server;	//the socket of the server
    private int port = 7777;		//port of the Server - client need to connect with the same port!
    public ServerMain() {
        try {
            server = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * handle all the connections.
     * @param args
     */
    public static void main(String[] args) {
    	new ServerMain().handleConnection();
    }

    public void handleConnection() {
        System.out.println("Waiting for client message...");
        /* The server do a loop here to accept all connection initiated by the
        client application*/
        while (true) {
            try {
                Socket socket = server.accept();
                new ServerCommunicator(socket); //when wake up open the request in another thread
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}